home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / STREAM.CC < prev    next >
C/C++ Source or Header  |  1992-03-29  |  3KB  |  120 lines

  1. #include <stdarg.h>
  2. #include "ioprivate.h"
  3. #include "stream.h"
  4. #include "strstream.h"
  5.  
  6. static char Buffer[BUFSIZ];
  7. #define EndBuffer (Buffer+BUFSIZ)
  8. static char* next_chunk = Buffer; // Start of available part of Buffer.
  9.  
  10. char* form(const char* format, ...)
  11. {
  12.     int space_left = EndBuffer - next_chunk;
  13.     if (space_left < (BUFSIZ>>2)) // If less that 25% of the space is available
  14.     next_chunk = Buffer; // start over.
  15.     char* buf = next_chunk;
  16.  
  17.     strstreambuf stream(buf, EndBuffer-buf-1, buf);
  18.     va_list ap;
  19.     va_start(ap, format);
  20.     int count = stream.vform(format, ap);
  21.     va_end(ap);
  22.     stream.sputc(0);
  23.     next_chunk = buf + stream.pcount();
  24.     return buf;
  25. }
  26.  
  27. #define u_long unsigned long
  28.  
  29. static char* itoa(unsigned long i, int size, int neg, int base)
  30. {
  31.     // Conservative estimate: If base==2, might need 8 characters
  32.     // for each input byte, but normally 3 is plenty.
  33.     int needed = size ? size
  34.     : (base >= 8 ? 3 : 8) * sizeof(unsigned long) + 2;
  35.     int space_left = EndBuffer - next_chunk;
  36.     if (space_left <= needed)
  37.     next_chunk = Buffer; // start over.
  38.  
  39.     char* buf = next_chunk;
  40.  
  41.     register char* ptr = buf+needed+1;
  42.     next_chunk = ptr;
  43.  
  44.     if (needed < (2+neg) || ptr > EndBuffer)
  45.     return NULL;
  46.     *--ptr = 0;
  47.     if (neg)
  48.     *--ptr = '-';
  49.     
  50.     if (i == 0)
  51.     *--ptr = '0';
  52.     while (i != 0 && ptr > buf) {
  53.     int ch = i % base;
  54.     i = i / base;
  55.     if (ch >= 10)
  56.         ch += 'a' - 10;
  57.     else
  58.         ch += '0';
  59.     *--ptr = ch;
  60.     }
  61.     if (size == 0)
  62.     return ptr;
  63.     while (ptr > buf)
  64.     *--ptr = ' ';
  65.     return buf;
  66. }
  67.  
  68. char* dec(long i, int len /* = 0 */)
  69. {
  70.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  71.     else return itoa((unsigned long)(-i), len, 1, 10);
  72. }
  73. char* dec(int i, int len /* = 0 */)
  74. {
  75.     if (i >= 0) return itoa((unsigned long)i, len, 0, 10);
  76.     else return itoa((unsigned long)(-i), len, 1, 10);
  77. }
  78. char* dec(unsigned long i, int len /* = 0 */)
  79. {
  80.     return itoa(i, len, 0, 10);
  81. }
  82. char* dec(unsigned int i, int len /* = 0 */)
  83. {
  84.     return itoa(i, len, 0, 10);
  85. }
  86.  
  87. char* hex(long i, int len /* = 0 */)
  88. {
  89.     return itoa((unsigned long)i, len, 0, 16);
  90. }
  91. char* hex(int i, int len /* = 0 */)
  92. {
  93.     return itoa((unsigned long)i, len, 0, 16);
  94. }
  95. char* hex(unsigned long i, int len /* = 0 */)
  96. {
  97.     return itoa(i, len, 0, 16);
  98. }
  99. char* hex(unsigned int i, int len /* = 0 */)
  100. {
  101.     return itoa(i, len, 0, 16);
  102. }
  103.  
  104. char* oct(long i, int len /* = 0 */)
  105. {
  106.     return itoa((unsigned long)i, len, 0, 8);
  107. }
  108. char* oct(int i, int len /* = 0 */)
  109. {
  110.     return itoa((unsigned long)i, len, 0, 8);
  111. }
  112. char* oct(unsigned long i, int len /* = 0 */)
  113. {
  114.     return itoa(i, len, 0, 8);
  115. }
  116. char* oct(unsigned int i, int len /* = 0 */)
  117. {
  118.     return itoa(i, len, 0, 8);
  119. }
  120.